﻿using RabbitMQ_Service.MessageTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Utility.RabbitMQ_Service;
using Utility.RabbitMQ_Service.Implementations;

namespace Manufacturing_Planning_System_Server
{
    internal class RunningMPS
    {
        private readonly String systemName;

        private RunningMPS(String systemName)
        {
            this.systemName = systemName;
        }

        public static RunningMPS NewRunningMPS(String systemName)
        {
            return new RunningMPS(systemName);
        }

        public void StartSystem()
        {
            Console.WriteLine(this.systemName + " wurde gestartet.");

            using (RabbitMQService service = RabbitMQServiceImpl.CreateRabbitMQService())
            {
                using (RabbitMQQueue<IAmAliveMessage> queue = service.NewRabbitMQQueue<IAmAliveMessage>("monitor.iamalive"))
                {
                    try
                    {
                        queue.Purge();

                        while (true)
                        {
                            IAmAliveMessage ok = new IAmAliveMessage
                            {
                                SystemName = this.systemName,
                                Ok = true
                            };

                            queue.Send(ok);
                            Thread.Sleep(1000);
                        }
                    } catch (ThreadInterruptedException) {
                        Console.WriteLine(this.systemName + " wurde beendet.");
                    }
                }
            }
        }
    }
}
